package SolodkovDA.pages;

import static org.openqa.selenium.support.ui.ExpectedConditions.presenceOfElementLocated;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

public class FilmViewPage extends InternalPage{
	
	public FilmViewPage (PageManager pages) {
		super(pages);
	}
	
	  private boolean acceptNextAlert = true;
	 
	@FindBy(css = "img[alt=\"Remove\"]")
	  private WebElement removeButton;
	
	  public FilmViewPage ensurePageLoaded() {
		    super.ensurePageLoaded();
		    wait.until(presenceOfElementLocated(By.cssSelector("img[alt=\"Remove\"]")));
		    return this;
		  }
	  
	  public boolean clickRemoveButton() {
		  removeButton.click();
		  return closeAlertAndGetItsText().matches("^Are you sure you want to remove this[\\s\\S]$");
		  }
	  
	  private String closeAlertAndGetItsText() {
		    try {
		      Alert alert = driver.switchTo().alert();
		      String alertText = alert.getText();
		      if (acceptNextAlert) {
		        alert.accept();
		      } else {
		        alert.dismiss();
		      }
		      return alertText;
		    } finally {
		      acceptNextAlert = true;
		    }
		  }
	  
}
